package cn.max.plat.db;

import cn.max.SystemProperties;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;

import java.net.UnknownHostException;

/**
 * Created by Max-Goday on 15/7/23.
 */


public class DBManager {

	private static MongoClient mongoClient;

	private static String REPOSITORY_REPO_SCHEMA = "Repository";
	private static String CLUSTER_REPO_SCHEMA = "Cluser";

	static {
		try {
			mongoClient = new MongoClient(SystemProperties.get("mongodb.ip"), Integer.parseInt(SystemProperties.get("mongodb.port")));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public static DB getMongoDB() {
		return mongoClient.getDB(SystemProperties.get("mongodb.db"));
	}

	public static DBCollection getRepositoryRepo() {
		return getMongoDB().getCollection(REPOSITORY_REPO_SCHEMA);
	}

	public static DBCollection getClusterRepo() {
		return getMongoDB().getCollection(CLUSTER_REPO_SCHEMA);
	}

	public static void main(String[] args) {

		try {

			DB db = mongoClient.getDB("AutoDp");

			DBCollection coll = db.getCollection("repository");

			DBCursor cursor = coll.find(new BasicDBObject("name", new BasicDBObject("$exists", true)));


			try {
				while(cursor.hasNext()) {
					System.out.println(cursor.next());

				}
			} finally {
				cursor.close();
			}

			coll.remove(new BasicDBObject("name", new BasicDBObject("$exists", true)));

			mongoClient.close();

		} catch (Exception e) {
			e.printStackTrace();
		}


	}


}
